package com.jiehoo.ooqi;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Search info, used by client or public service interface most time, only provide additional conditions, sorts and pagination info.
 */
public class SearchInfo implements Serializable {
  private static final long serialVersionUID = -1338139177191173413L;

  /**
   * Additional conditions for some query info provided by service.
   */
  private List<Condition> conditions = new ArrayList<Condition>();

  /**
   * Sort info.
   */
  private List<Sort> sorts = new ArrayList<Sort>();

  /**
   * Page size.
   */
  private int pageSize = Constants.DEFAULT_PAGE_SIZE;

  /**
   * First return row.
   */
  private int firstRow;

  /**
   * Whether need return total rows.
   */
  private boolean getTotalRows;

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }

  public int getFirstRow() {
    return firstRow;
  }

  public void setFirstRow(int firstRow) {
    this.firstRow = firstRow;
  }

  public int getEndRow() {
    return firstRow + pageSize;
  }

  public List<Condition> getConditions() {
    return conditions;
  }

  public void setConditions(ArrayList<Condition> conditions) {
    this.conditions = conditions;
  }

  public void addCondition(Condition condition) {
    conditions.add(condition);
  }

  public void addConditions(List<Condition> conditions) {
    this.conditions.addAll(conditions);
  }

  public void addConditions(Condition[] conditions) {
    this.conditions.addAll(Arrays.asList(conditions));
  }

  public Condition addAndCondition() {
    Condition and = new Condition((Field) null, Operator.and,
        new ArrayList<Condition>());
    conditions.add(and);
    return and;
  }

  public Condition addOrCondition() {
    Condition or = new Condition((Field) null, Operator.or,
        new ArrayList<Condition>());
    conditions.add(or);
    return or;
  }

  public void addNotCondition(Condition condition) {
    Condition not = new Condition((Field) null, Operator.not, condition);
    conditions.add(not);
  }

  public void addInCondition(String name, List<?> list) {
    Condition in = new Condition(name, Operator.in, list);
    conditions.add(in);
  }

  public void addInCondition(String name, Object[] array) {
    addInCondition(name, Arrays.asList(array));
  }

  public List<Sort> getSorts() {
    return sorts;
  }

  public void setSorts(List<Sort> sorts) {
    this.sorts = sorts;
  }

  public void addSort(String name, boolean asc) {
    sorts.add(new Sort(name, asc));
  }

  public void addSort(String name) {
    sorts.add(new Sort(name, true));
  }
  
  public void addSort(Sort sort)
  {
	  sorts.add(sort);
  }
  
  public void addSorts(List<Sort> sorts)
  {
	  this.sorts.addAll(sorts);
  }

  public boolean isGetTotalRows() {
    return getTotalRows;
  }

  public void setGetTotalRows(boolean getTotalRows) {
    this.getTotalRows = getTotalRows;
  }

  public String toString() {
    StringBuilder buffer = new StringBuilder();
    buffer.append(getClass().getSimpleName()).append("-")
        .append(" conditions:");
    for (Condition condition : conditions) {
      buffer.append(condition).append(",");
    }
    buffer.deleteCharAt(buffer.length() - 1);
    buffer.append("; sorts:");
    for (Sort sort : sorts) {
      buffer.append(sort).append(",");
    }
    buffer.deleteCharAt(buffer.length() - 1);
    buffer.append("; pageSize:").append(pageSize);
    buffer.append("; firstRow:").append(firstRow);
    buffer.append("; getTotalRows:").append(getTotalRows);
    return buffer.toString();
  }
}
